package com.android.baselibrary.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

public abstract class BaseCustomView<S extends BaseCustomViewModel> extends LinearLayout implements ICustomView<S>, View.OnClickListener {

    private ViewDataBinding dataBinding;
    private S viewModel;

    public BaseCustomView(Context context) {
        super(context);
        init();
    }

    public BaseCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BaseCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public BaseCustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public View getRootView() {
        return dataBinding.getRoot();
    }

    public void init() {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (setViewLayoutId() != 0) {
            dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), setViewLayoutId(), this, true);
            setLayoutParams(dataBinding.getRoot().getLayoutParams());
           // this.addView(dataBinding.getRoot(),dataBinding.getRoot().getLayoutParams());
            dataBinding.getRoot().setOnClickListener(this);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int childHeight = 0;
        int childWidth = 0;

        ViewGroup.LayoutParams layoutParams = getChildAt(0).getLayoutParams();
        int childMeasureWidthSpec = getChildMeasureSpec(widthMeasureSpec, getChildAt(0).getPaddingLeft() + getChildAt(0).getPaddingEnd(), layoutParams.width);
        int childMeasureHeightSpec = getChildMeasureSpec(heightMeasureSpec, getChildAt(0).getPaddingTop() + getChildAt(0).getPaddingBottom(), layoutParams.height);
        getChildAt(0).measure(childMeasureWidthSpec, childMeasureHeightSpec);

        childWidth = Math.max(getChildAt(0).getMeasuredWidth(), childWidth);
        childHeight = Math.max(getChildAt(0).getMeasuredHeight(), childHeight);
        Log.i("TAG--110--", "onMeasure: getMeasuredWidth"+childWidth+",getMeasuredHeight:"+childHeight);

        setMeasuredDimension(
                widthMode == MeasureSpec.EXACTLY ? widthSize : childWidth,
                heightMode == MeasureSpec.EXACTLY ? heightSize : childHeight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void setData(S data) {
        viewModel = data;
        if (dataBinding != null) {
            dataBinding.setVariable(getBR() ,viewModel);
            dataBinding.executePendingBindings();
        }
        initView(viewModel);
    }

    protected ViewDataBinding getDataBinding() {
        return dataBinding;
    }

    protected S getViewModel() {
        return viewModel;
    }


    protected abstract int setViewLayoutId();

    protected abstract void initView(S data);
    protected abstract int getBR();
}
